/* nixonFTP
 * FTP client version 0.1
 * Copyright (C) 2010 NIXON Development Corporation.
 * All rights reserved.
 * http://members.shaw.ca/nixon.com
 */
package nixonftp.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.Vector;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

/**
 *
 * @author Billy
 */
public class NXLogPanel extends JPanel {

	private JTextPane a;
	private JScrollPane spa;

	private int initialLeft;

	public NXLogPanel() {
		super();
		setLayout(new BorderLayout());
		setUp();
	}

	public void setUp() {
		a = new JTextPane();
		//a.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		addStyles(a.getStyledDocument());
		a.setEditable(false);
		a.setBackground(Color.WHITE);
		a.setOpaque(true);

		spa = new JScrollPane(a);
		add(spa);
		//add(spb);
		spa.setBorder(null);
	}

	private void addStyles(StyledDocument doc) {
		Style def = StyleContext.getDefaultStyleContext().
				  getStyle(StyleContext.DEFAULT_STYLE);

		Style command = doc.addStyle("command", def);
		StyleConstants.setForeground(command, Color.BLACK);

		//responses

		Style regular = doc.addStyle("regular", command);
		StyleConstants.setForeground(regular, Color.GRAY);
		StyleConstants.setFontFamily(regular, "Monospaced");

		Style s = doc.addStyle("tryagain", command);
		StyleConstants.setForeground(s, Color.ORANGE);

		s = doc.addStyle("pending", command);
		StyleConstants.setForeground(s, new Color(51, 102, 153));

		s = doc.addStyle("error", command);
		StyleConstants.setForeground(s, Color.RED);

		s = doc.addStyle("done", command);
		StyleConstants.setForeground(s, new Color(0, 153, 0));
		StyleConstants.setFontFamily(s, "Monospaced");
	}

	public JTextPane getLog() {
		return a;
	}

	public void displayText(Vector<String> s) {
		if (s == null) {
			a.setText("");
			return;
		}

		Object[] lines = s.toArray();
		String line;

		boolean eraseControl = false;

		try {
			for (int x = 0; x < lines.length; x++) {
				line = (String) lines[x];
				appendText(line);
			}
		} catch (Exception e) {
		}

		setUp();
	}

	public void appendText(String text) {
		String style;
		boolean eraseControl;
		StyledDocument doc = a.getStyledDocument();
		String erase = null;

		if (text.startsWith("\u3000")) {
			style = "error";
			erase = "\u3000";
			eraseControl = true;
		} else if (text.startsWith("\u3001")) {
			style = "tryagain";
			erase = "\u3001";
			eraseControl = true;
		} else if (text.startsWith("\u3002")) {
			style = "pending";
			erase = "\u3002";
			eraseControl = true;
		} else if (text.startsWith("\u3003")) {
			style = "done";
			erase = "\u3003";
			eraseControl = true;
		} else if (text.startsWith("\u3004")) {
			style = "regular";
			erase = "\u3004";
			eraseControl = true;
		} else {
			style = "default";
			eraseControl = false;
		}

		if (eraseControl) {
			text = text.replaceAll(erase, "");
		}
		//String end = (style.equals("default")) ? "" : "\n";
		try {
			doc.insertString(doc.getLength(), text + '\n', doc.getStyle(style));
		} catch (BadLocationException ex) {
		}
	}
}
